package com.linkage.jz.bankruptcy.activity.Main;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.linkage.jz.bankruptcy.BankruptcyApp;
import com.linkage.jz.bankruptcy.Consts;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.activity.Person.PersonSettingReadMeActivity;
import com.linkage.jz.bankruptcy.base.BaseActivity;
import com.linkage.jz.bankruptcy.utils.SPUtils;
import com.linkage.jz.bankruptcy.utils.Utils;
import com.linkage.jz.bankruptcy.widget.MyCommonDialog;
import com.linkage.jz.bankruptcy.widget.ShapeLoadingDialog;

import org.json.JSONObject;

import java.io.IOException;
import java.util.Random;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


/**
 * A login screen that offers login via email/password.
 */
public  class LawDetailActivity extends BaseActivity implements OnClickListener{
    private static final String TAG = LawDetailActivity.class.getSimpleName();
    private MyCommonDialog dialog;
    private ShapeLoadingDialog shapeLoadingDialog ;

    private String mID,mContent;
    @Override
    public void initData() {

    }

    @Override
    public void setListener() {
//        r_tru.setOnClickListener(this);
        top_back.setOnClickListener(this);
//        r_getver.setOnClickListener(this);
    }

    @Override
    public void initTitle() {
        top_title = findViewById(R.id.top_title);
        top_title.setText("参考法规");
        top_mune.setVisibility(View.INVISIBLE);
    }

    @Override
    public void initView() {

    }


    TextView top_title,tv_title,tv_content;
    Button top_back,top_mune;
//    RelativeLayout r_tru,r_getver;
    String title;

    public static void start(Context context,String title,String id,String content) {
        Intent intent = new Intent(context, LawDetailActivity.class);
        intent.putExtra("title",title);
        intent.putExtra("id",id);
        intent.putExtra("content",content);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        setContentView(R.layout.activity_law_detail);

        initview();
        shapeLoadingDialog = new ShapeLoadingDialog(this);

        Intent intent = getIntent();
        title = intent.getStringExtra("title");
        mID = intent.getStringExtra("id");
        mContent = intent.getStringExtra("content");

        tv_title.setText(title);
        tv_content.setText(Html.fromHtml(mContent));
//        getData();
        super.onCreate(savedInstanceState);
    }

    //初始化视图
    private void initview() {
        //登录层、下拉层、其它登录方式层
        tv_content = findViewById(R.id.tv_content);
        tv_content.setMovementMethod(ScrollingMovementMethod.getInstance());
        tv_title = findViewById(R.id.tv_title);
//        tv_ver = findViewById(R.id.tv_ver);
        top_back = findViewById(R.id.top_back);
        top_mune = findViewById(R.id.top_mune);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.top_back:
                finish();
                break;
            case R.id.r_tru:
                PersonSettingReadMeActivity.start(this);
                break;

            case R.id.r_getver:
//                getVer();
//                getTest();
                break;

            default:
                break;
        }
    }

    private int update(String url) {
        int ret = -1;
        try {
            Uri uri = Uri.parse(url);
            startActivity(new Intent(Intent.ACTION_VIEW, uri));
            ret = 0;
        } catch (Exception e) {

        }
        return ret;
    }



    public void getData()
    {
        if(BankruptcyApp.tokeninfo == null|| BankruptcyApp.tokeninfo.getToken() == null)
        {
            return;
        }
        shapeLoadingDialog.setLoadingText("加载中...");
        shapeLoadingDialog.setCanceledOnTouchOutside(false);
        shapeLoadingDialog.show();
        OkHttpClient okHttpClient = new OkHttpClient();
//        HashMap<String, String> params = new HashMap<String, String>();
        Random random = new Random();
        String rom = random.nextLong()+"";
        String timestamp =System.currentTimeMillis()+"";
        String userToken = SPUtils.getInstance(Consts.SPNAME).getString("userToken","");
        String userId = SPUtils.getInstance(Consts.SPNAME).getString("userId","");

        final String verName = Utils.getLocalVersionName(this)+"";
        RequestBody requestBody = new FormBody.Builder()
                .add("appid","1000000001")
                .add("nonce",rom)
                .add("timestamp", timestamp)

                .add("token", BankruptcyApp.tokeninfo.getToken())
//                .add("token", "")
                .add("id", mID)
                .build();

        String url = Consts.WEBURL_INSTRDETAIL;
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                shapeLoadingDialog.dismiss();
                Log.d(TAG, "onFailure: " + e.getMessage());
                runOnUiThread(new Runnable(){

                    @Override
                    public void run() {
                        //更新UI


                    }

                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.d(TAG, response.protocol() + " " +response.code() + " " + response.message());
                Headers headers = response.headers();
                shapeLoadingDialog.dismiss();
                for (int i = 0; i < headers.size(); i++) {
                    Log.d(TAG, headers.name(i) + ":" + headers.value(i));
                }


                String str = response.body().string();
                try{
                    JSONObject object = new JSONObject(str);
                    JSONObject data = object.optJSONObject("data");

                    if(object.optInt("code") == 0)
                    {

                            final String strTitle = data.optString("title");
                            final String content = data.optString("content");
                            runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    //更新UI
                                    tv_content.setText(Html.fromHtml(content));
                                    tv_title.setText(strTitle);
                                }

                            });

//                        finish();


                    }else {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                //更新UI
                                Toast.makeText(LawDetailActivity.this, "获取失败", Toast.LENGTH_SHORT).show();

                            }

                        });

                    }
                    str = "";
                }catch (Exception ex)
                {
                    ex.printStackTrace();
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            //更新UI
                            Toast.makeText(LawDetailActivity.this, "获取失败", Toast.LENGTH_SHORT).show();

                        }

                    });
                }
            }
        });
    }



}

